<template>
  <div class="helps">
    <!-- 头部区域 -->
    <div class="tabBar">
        <div v-for="(item, index) in navLists"
        :key="index" class="boxNoClick" :class="{boxClick:changeStyle == index}" @click="isShow(index)">{{item.text}}</div>
    </div>

    <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多数据了"
        @load="onLoad"
      >
      <!-- 待审主要内容区 -->
      <ul v-show="changeStyle == 0" class="leftContent" v-for="item in list">
          <li  @click="jump(item.id)">
            <p><b class="fontSize">{{item.userName}}</b><span>补充医疗互助申请<img src="../imgs/more.png" alt=""></span></p>

            <p class="times">申请时间：{{item.createTime | formatDate}}<span class="shenhe" @click.stop="itemData = item,isAudit = true">审核</span></p>
          </li>
      </ul>

     <!-- 已审主要内容区 -->
      <ul v-show="changeStyle == 1" class="leftContent" v-for="item in list" >
          <li  @click="jump(item.id)">
            <p><b class="fontSize">{{item.userName}}</b><span>补充医疗互助申请<img src="../imgs/more.png" alt=""></span></p>

            <p class="times">申请时间：{{item.createTime | formatDate}}<span class="shenheTG">审核通过</span></p>
          </li>
      </ul>
    </van-list>

    <van-dialog closeOnClickOverlay v-model="isAudit" title="审核" confirm-button-text="通过" cancel-button-text="不通过" show-cancel-button @confirm="Audit" @cancel="Audit(3)">
      <p style="font-size:14px;text-align:center;padding: 10px 0">是否审核通过</p>
    </van-dialog>

  </div>
</template>

<script>
import Vue from "vue";
import api from "@/data/api/index.js";
import { Dialog } from 'vant'
import { Notify } from 'vant';

// 全局注册
Vue.use(Notify);
Vue.use(Dialog);
export default {
  //过滤器
  filters:{
    formatDate(date) {
      const nDate = new Date(date)
      const y = nDate.getFullYear()
      const m = (nDate.getMonth()+1).toString().padStart(2,0)
      const d = nDate.getDate().toString().padStart(2,0)
      return y+'-'+m+'-'+d
    }
  },
  data() {
    return{
      navLists:[
          {
            "text":"待审"          
          },
          {
            "text":"已审"          
          }
        ],
      changeStyle:0,
      count: 0,
      finished: false,
      loading: false,
      // 全部列表
      list: [],
      params:{
        page:1,
        rows:10
      },
      itemData:{},
      isAudit: false
    }
  },
  mounted () {
  },
  methods: {
    async onLoad(){
      await this.getList()
      this.loading = false;
      // 数据全部加载完成
      if (this.list.length >= this.count) {
        this.finished = true;
      }
    },
    isShow(index) {
      this.params.page = 1
      this.list = []
      this.changeStyle = index 
      this.onLoad()
    },
     // 审核
    Audit(status){
      let url,data,state;
      if(this.itemData.status == 0) {
        url = "first"
      } else if(this.itemData.status == 1) {
        url = "finally"
      }
      if(status == 3){
        if (this.itemData.status == 1) {
          state = 3;
        } else {
          state = 4;
        }
      } else {
        if (this.itemData.status == 0) {
          state = 1;
        } else {
          state = 2;
        }
      }
      data = {
        id: this.itemData.id,
        status: state
      }
      api.SMTMAAFAudit(url, {}, data).then(res => {
        if (res.code == "success") {
          Notify({ type: 'success', message: '审批成功' });
        } else {
          Notify({ type: 'danger', message: res.desc });
        }
        this.isAudit = false;
        this.params.page = 1
        this.list = []
        this.getList()
      });
    },
    getList() {
      return new Promise((resolve, reject)=>{
        if(this.changeStyle == 0){
          api.SMTMAAFGetAuditList(this.params).then(res => {
            this.list.push(...res.data)
            this.count = res.count
            resolve(res)
          })
          .catch(error => {
            reject(error);
          });
        } else {
          api.SMTMAAFGetAuditedList(this.params).then(res => {
            this.list.push(...res.data)
            this.count = res.count
            resolve(res)
          })
          .catch(error => {
            reject(error);
          });
        }
      })
    },
    jump(id){
      this.$troute.query('mySupDetail', {id:id});
    }
  }
}
</script>

<style lang="scss" scoped>
.helps{
  margin: 0 20px;
  .tabBar{
    width: 100%;
    height: 40px;
    display: flex;
    .boxClick{
      width: 40px;
      height: 100%;
      font-size: 20px;
      font-weight: bold;
      border-bottom: 4px solid rgb(201,21,30);
    }
    .boxNoClick{
      width: 40px;
      height: 100%;
      font-size: 20px;
      margin-left: 20px;
      text-align: center;
    }
  }
  .leftContent li {
    height: 70px;
    margin-top: 20px;
    padding: 15px 10px 20px 20px;
    background-size: 50px;
    border-radius: 10px;
    box-shadow: 2px 2px 8px #f2f2f2, -2px -2px 8px #f2f2f2;
    p{
        font-weight: bold;
      .fontSize{
        font-size: 20px;
      }
      span{
        font-weight: bold;
        padding: 2px 8px;
        float: right;
        img{
        margin-left: 6px;
        width: 8px;
      }
      }
      
    }
    .times{
      margin-top: 17px;
      font-weight: normal;
      color: rgb(129,129,129);
      .shenhe{
      width: 70px;
      height: 32px;
      font-weight: bold;
      background-color: rgb(159,16,22);
      border-radius: 18px;
      text-align: center;
      line-height: 32px;
      color: #fff;
      float: right;
    }
    .shenheTG{
      font-weight: bold;
      color: rgb(159,16,22);
      float: right;
      font-size: 15px;
    }
    }
  }
}
</style>